/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package anm_banvetau;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author GREEN
 */
public class ANM_BanVeTau {

    /**
     * @param args the command line arguments
     */
    public static String user = "sa";
    public static String password = "";
    public static String servername = "GREEN-PC";
    public static Connection Connect(String servername,String user, String password) throws Exception {
        Connection con = null;
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection("jdbc:sqlserver://" + servername + ";databaseName=QL_VeTau;user=" + user + ";password=" + password + "");
        if(con != null)
            System.out.println("Connection Successful!");  

        return con;
    }
    
    int KT(Vector kt, String servername,String user, String password) throws SQLException, Exception {
        Connection con = Connect(servername,user, password);
        if (con != null) {
            PreparedStatement ps;
            String query;
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select TrangThai from HocSinh where TenDangNhap=" + "'" + kt.get(0) + "'" + " and MatKhau=" + "'" + kt.get(1) + "'" + "");
            if(rs.getBoolean("TrangThai") == false){
                query = ("update NGUOIDUNG set TrangThai=1 where Ten_DN= " + kt.get(0) + "");
                ps = con.prepareStatement(query);
                //System.out.println("Đăng Nhập Thành Công!");
                return 1;
            }
            //System.out.println("Đăng Nhập Thất Bại!");
        }
        return 0;
    }
    
    void LayDS(DefaultTableModel mode, Vector rowData, String servername,String user, String password) throws SQLException, IOException, Exception {
        Connection con = Connect(servername,user, password);
        if (con != null) {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select Ma_ChuyenTau,SL_Ve,KhoiHanh,GaDi,GaDen,SoVeDuocMua,SoVeConTrong from CHUYENTAU");
            while (rs.next()) {

                int MaCT = rs.getInt("Ma_ChuyenTau");
                int SLve = rs.getInt("SL_Ve");
                String KhoiHanh = rs.getString("KhoiHanh");
                String GaDi = rs.getString("GaDi");
                String GaDen = rs.getString("GaDen");
                int SoVeDuocMua = rs.getInt("SoVeDuocMua");
                int SoVeTrong = rs.getInt("SoVeConTrong");
                rowData = new Vector();
                rowData.add(MaCT);
                rowData.add(SLve);
                rowData.add(KhoiHanh);
                rowData.add(GaDi);
                rowData.add(GaDen);
                rowData.add(SoVeDuocMua);
                rowData.add(SoVeTrong);
                mode.addRow(rowData);
            }
            con.close();
        }
    }
    
    void JTable(Vector rowHeader, DefaultTableModel mode1, Vector rowData, JTable jtbDS, String servername,String user, String password) throws SQLException, IOException, Exception {
        Connection con = Connect(servername,user, password);
        if (con != null) {
            rowHeader = new Vector();
            rowHeader.add("Mã CT");
            rowHeader.add("SL Vé");
            rowHeader.add("TG Chạy");
            rowHeader.add("Ga Đi");
            rowHeader.add("Ga Đến");
            rowHeader.add("Số Vé Được Mua");
            rowHeader.add("Số Vé Trống");
            mode1 = new DefaultTableModel(rowHeader, 0);
            jtbDS.setModel(mode1);
            rowData = new Vector();
            LayDS(mode1, rowData, servername,user, password);
        }
        con.close();
    }
    
    String KT_Them(Vector ct, String servername,String user, String password) {
        String str = "";
         
        if (ct.get(0).toString().compareTo("") == 0) {
            str = str + "\n" + "+Số lượng vé";
        }
        if (ct.get(1).toString()== null) {
            str = str + "\n" + "+Thời gian chạy không được để trống";
        }
        if (ct.get(2).toString()== null) {
            str = str + "\n" + "+Ga đi không được để trống";
        }
        if (ct.get(3).toString()== null) {
            str = str + "\n" + "+Ga đến không được để trống";
        }
        if (ct.get(4).toString()== null) {
            str = str + "\n" + "+Số vé được mua không được để trống";
        }
        return str;
    }
    void Them(Vector rowData, FileInputStream fis, String servername,String user, String password) throws SQLException, Exception {
        Connection con = Connect(servername,user, password);
        if (con != null) {
            
            PreparedStatement ps;
            String query;
         
            query = "insert into CHUYENTAU(SL_Ve,KhoiHanh,GaDi,GaDen,SoVeDuocMua,SoVeConTrong) VALUES(?,?,?,?,?,?)";
            ps = con.prepareStatement(query);            
            ps.setString(1, rowData.get(0).toString());
            ps.setString(2, rowData.get(1).toString());
            ps.setString(3, rowData.get(2).toString());
            ps.setString(4, rowData.get(3).toString());
            ps.setString(5, rowData.get(4).toString());
            ps.setString(6, rowData.get(5).toString());
            
            ps.executeUpdate();    
        }
        con.close();
    }
    public static void Server()
    {
        try {
           // String cau;// Cau tu client gui toi
            //String ketQua = "";// Cau sau khi xu ly xong tra ve client
            ServerSocket ss = new ServerSocket(9999);// Tao cong 9999 de server lang nghe
            System.out.println("Server: Đã khởi động máy chủ");

            while (true)// Cho client ket noi
            {
                /*
                // Khong dung multithread
                Socket connectionSocket= ss.accept();
                BufferedReader fromClient= new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));// Tao input stream
                DataOutputStream sendToClient= new DataOutputStream(connectionSocket.getOutputStream());// Tao output stream
           //     Thread.sleep(5000);// Giả sử server phải xử lý mất 5s
                while (true)
                {
                    cau = fromClient.readLine();
                    System.out.println("FROM CLIENT: "+cau);// In ra chuỗi server nhận đc từ client
                    if (cau.equalsIgnoreCase("quit"))
                        break;

                    // Dao chuoi nhan duoc
                    StringBuilder str = new StringBuilder(cau);
                    ketQua = str.reverse().toString();

                    sendToClient.writeBytes(ketQua + '\n');
                }*/
                
                // Su dung multithread
                // Khi co 1 client gui yeu cau toi thi se tao ra 1 thread phuc vu client do
                new ThreadSocket(ss.accept()).start();
            }
        } catch (IOException e) {
            System.out.println("Exception: " + e.getMessage());
        }
         //catch (InterruptedException ie)
        {
        }
    }
    public static void main(String[] args){
        // TODO code application logic here
        ANM_BanVeTau s = new ANM_BanVeTau();
        //s.Server();
        frmServer frm = new frmServer();
        frm.setVisible(true);   
    }
}
